package apiUtils.params;

import android.annotation.SuppressLint;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import appUtils.PayDESUtil;
import appUtils.PayMD5Util;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/5/4.
 */

public class DESParams extends Params {
    private final JSONObject object = new JSONObject();

    @Override
    public String toString() {
        @SuppressLint("SimpleDateFormat")
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String key = PayMD5Util.getMD5(dateFormat.format(new Date()));
        return "?timeStamp=" + key + "&msg=" + PayDESUtil.encode(key, object.toString());
    }

    @Override
    public void put(String key, String value) throws JSONException {
        object.put(key, value);
    }

    @Override
    public RequestBody getRequestBody() {
        @SuppressLint("SimpleDateFormat")
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String nonce = PayMD5Util.getMD5(dateFormat.format(new Date()));
        String encodeMsg = PayDESUtil.encode(nonce, object.toString());
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("timeStamp", nonce);
        builder.add("msg", encodeMsg);
        return builder.build();
    }
}
